%%%-------------------------------------------------------------------
%%% @author
%%% @copyright (C) 2013, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 02. Nov 2013 11:09
%%%-------------------------------------------------------------------
-module(koordinator).
-export([dict_init/1,dict_findMinEdge/1,dict_findMinEdge/2,dict_findEdgeNodeName/2,nodeDict_set/3,dict_getBranchEdges/2,dict_containStateEdges/2,isEdgeEqual/2]).

dict_init(ListNighbor) ->
  NewListNighbor = dict:from_list(ListNighbor) ,
  Acc0 = dict:new(),
  Fun = fun(Key, Value, Accu) ->
  dict:store(Key, {Value, basic}, Accu)
  end,
  dict:fold(Fun, Acc0, NewListNighbor).

dict_findMinEdge(ListNighbor) ->
  %Values = [V || {_,V} <- dict:to_list(NoteEdges)],
  Keys = [K || {K,_} <- dict:to_list(ListNighbor)],
  %{Keys, Values} = lists:unzip(dict:to_list(NoteEdges)),
  lists:min(Keys).

dict_findMinEdge( ListNighbor, StateFilter ) ->
  Pred =  fun(_,V) ->
    { _Edge, EdgeState } = V,
    ( EdgeState =:= StateFilter)
  end,
  Dict = dict:filter(Pred, ListNighbor),
  Keys = [K || {K,_} <- dict:to_list(Dict)],
  lists:min(Keys).

dict_findEdgeNodeName(ListNighbor,Edge) ->
  {ok, {Values,State}} = dict:find(Edge,ListNighbor),
  {ok, {Values,State}}.

nodeDict_set(KeyEdge, ValueState,NodeDict) ->
  NewDict = dict:store( KeyEdge, ValueState ,NodeDict),
  NewDict.

dict_getBranchEdges( Dict, UnequalEdge) ->
  Pred =  fun(K, V) ->
    { _Values, State } = V,
    ( (State == branch) and (K /= UnequalEdge))
  end,
  dict:filter(Pred, Dict).
  
dict_containStateEdges(NodeDict,PState) ->
  Pred =  fun(_,V) ->
                    { _Values, State } = V,
                    ( State =:= PState)
            end,
  NewDict = dict:filter(Pred, NodeDict),
  (dict:size(NewDict) > 0).


isEdgeEqual( Edge1, Edge2 ) ->
  if(Edge1 == Edge2) ->
    true;
  true ->
    false
  end
.